<?php
namespace backend\controllers;
use backend\base\BaseService;
use backend\models\OssModel;
use Yii;
use backend\base\BaseController;
use yii\data\Pagination;
use backend\models\SlidersImageModel;

/**
 * 网站管理
 * Agent controller
 */
class WebController extends BaseController
{
    /**
     * 首页轮播图列表
     * @return string
     */
    public function actionSlidersImageList()
    {
        //查询
        $query = SlidersImageModel::find()->where(['is_delete'=>0]);
        $count = $query->count();
        //分页
        $pagination = new Pagination(['totalCount' => $count]);
        //列表
        $list = $query->offset($pagination->offset)
                    ->limit($pagination->limit)
                    ->orderBy('sort desc,id desc')
                    ->all();
        return $this->render('sliders_image_list',['list' => $list,'pagination'=>$pagination]);
    }

    /**
     * 首页轮播图添加
     * @return string
     */
    public function actionSlidersImageAdd()
    {
        $model = new SlidersImageModel();
        if(Yii::$app->request->isPost){
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                $this->addRecord('添加首页轮播图',$model->id);
                return $this->saveBack(['sliders-image-list']);
            }
        }
        //获取上传参数
        $ossUpload = OssModel::getOssUpload();
        return $this->render('sliders_image_add',['model'=>$model,'ossUpload' => $ossUpload]);
    }

    /**
     * 首页轮播图编辑
     * @return string
     */
    public function actionSlidersImageEdit($id)
    {
        $model = SlidersImageModel::findOne($id);
        if(Yii::$app->request->isPost){
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                $this->addRecord('编辑首页轮播图',$id);
                return $this->saveBack(['sliders-image-list']);
            }
        }
        //获取上传参数
        $ossUpload = OssModel::getOssUpload();
        return $this->render('sliders_image_edit',['model'=>$model,'id'=>$id,'ossUpload' => $ossUpload]);
    }

    /**
     * 首页轮播图删除
     * @return string
     */
    public function actionSlidersImageDeleAjaxDo()
    {
        $data = $this->body;
        $model = SlidersImageModel::findOne(intval($data['id']));
        if($model){
            $model->is_delete = 1;
            if($model->save()){
                $this->addRecord('删除首页轮播图',$data['id']);
                return json_encode(parent::ajaxReturn('operate_success', []));
            }
        }
        return json_encode(parent::ajaxReturn('operate_error_delete', []));
    }

}
